package com.store.app.activity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.bean.BitmapBean;
import com.store.app.bean.CouponBean;
import com.store.app.bean.CouponUpdateBean;
import com.store.app.http.Stores;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.stores.MultipartEntity;
import com.store.app.utils.GsonUtil;
import com.store.app.utils.MoneyUtil;
import com.store.app.utils.SettingData;
import com.store.app.utils.Utils;
import com.store.app.utils.UtilsBitmap;
import com.yalantis.ucrop.CropUtils;
import com.yalantis.ucrop.UCrop;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/4/12.
 * 免费领取型
 */
public class AddCoupons3_RebuildActivity extends BaseActivity implements IOnHttpListener {
    private LinearLayout right_lin;
    //标题
    private TextView title_tv, right_tv;
    //活动名称
    private EditText name_edit;
    //描述
    private EditText detail_edit;
    //消费最低金额
    private EditText minconsumption_edit;
    //发行量
    private EditText circulation_edit;
    //有效期
    private TextView data_tv;
    private ImageView img1, img2;
    //提交
    private TextView post_tv;
    //             活动名      描述                最低消费金额      发行量             有效期 限购
    private String s_name_edit, s_detail_edit, s_minconsumption, s_circulation_edit, s_data_tv, s_xiangou = "0";
    private int type = 1;
    private static final int FLAG_CHOOSE_IMG = 5;
    private static final int FLAG_CHOOSE_PHONE = 6;
    private static final int FLAG_MODIFY_FINISH = 7;
    private static final int MSG_IMG_FAILED = 1;
    private static final int MSG_IMG_SUCC = 2;
    private static String localTempImageFileName = "";
    private CouponBean couponBean;
    private String item_id;
    //上传的bitmap
    Bitmap newbitmap1 = null;
    Bitmap newbitmap2 = null;
    InputStream newBitmapIn1 = null;
    InputStream newBitmapIn2 = null;
    private int index = -1;     //标记上传的图片id

    private NetRequest netRequest;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {

                case MSG_IMG_FAILED:
                    dismissProgressDialog();
                    Toast.makeText(AddCoupons3_RebuildActivity.this, "图片上传失败！", Toast.LENGTH_SHORT).show();
                    break;
                case MSG_IMG_SUCC:
                    Log.i("mylog", "图片id " + (String) msg.obj);
                    netRequest.requestAddCoupon(1, SettingData.Post_AddCoupons3, s_name_edit, s_detail_edit,
                            (String) msg.obj, "0", "0", "0", "0", s_name_edit, "0", "0", s_minconsumption, s_circulation_edit, s_data_tv, s_xiangou);
                    break;
                default:
                    break;
            }
        }
    };
    private BitmapBean bitmapBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_addcoupons3);
        item_id = getIntent().getStringExtra("item_id");

        initView();
        initData();
        initListeners();
    }

    private void initData() {
        title_tv.setText("免费领取型");
        right_tv.setText("取消");
        right_tv.setVisibility(View.INVISIBLE);
        netRequest = new NetRequest(this);
        netRequest.requestCouponFindDetail(3, item_id);
        post_tv.setText("再发");


    }

    private void initListeners() {
        findViewById(R.id.public_ll_return).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showMyDialog(1);
            }
        });
        img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showMyDialog(2);
            }
        });
        data_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showDatePicker();
            }
        });
        post_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                s_name_edit = name_edit.getText().toString().trim();
                if (TextUtils.isEmpty(s_name_edit)) {
                    Toast.makeText(AddCoupons3_RebuildActivity.this, "请输入名称！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (s_name_edit.length() > 50) {
                    Toast.makeText(AddCoupons3_RebuildActivity.this, "输入名称字数过多！", Toast.LENGTH_SHORT).show();
                    return;
                }
                s_detail_edit = detail_edit.getText().toString().trim();
                if (TextUtils.isEmpty(s_detail_edit)) {
                    Toast.makeText(AddCoupons3_RebuildActivity.this, "请输入描述！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (s_detail_edit.length() > 200) {
                    Toast.makeText(AddCoupons3_RebuildActivity.this, "输入描述字数过多！", Toast.LENGTH_SHORT).show();
                    return;
                }

                s_circulation_edit = circulation_edit.getText().toString();
                if (TextUtils.isEmpty(s_circulation_edit)) {
                    Toast.makeText(AddCoupons3_RebuildActivity.this, "请输入发行量！", Toast.LENGTH_SHORT).show();
                    return;
                }
                s_circulation_edit = MoneyUtil.formatCount(s_circulation_edit);
                try {
                    Integer i_circulation_edit = Integer.parseInt(s_circulation_edit);
                    if (i_circulation_edit <= 0) {
                        Toast.makeText(AddCoupons3_RebuildActivity.this, "输入的发行量需大于0！", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } catch (Exception e) {
                    Toast.makeText(AddCoupons3_RebuildActivity.this, "输入的发行量需大于0！", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (s_circulation_edit.length() > 10) {
                    Toast.makeText(AddCoupons3_RebuildActivity.this, "您输入的数字过大！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(s_data_tv)) {
                    Toast.makeText(AddCoupons3_RebuildActivity.this, "请选择有效日期！", Toast.LENGTH_SHORT).show();
                    return;
                }
                s_minconsumption = minconsumption_edit.getText().toString();
                if (TextUtils.isEmpty(s_minconsumption)) {
                    Toast.makeText(AddCoupons3_RebuildActivity.this, "请输入最低消费金额！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (s_minconsumption.length() > 8) {
                    Toast.makeText(AddCoupons3_RebuildActivity.this, "您输入的数字过大！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (couponBean == null) {
                    Toast.makeText(AddCoupons3_RebuildActivity.this, "再发优惠券失败", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (newBitmapIn1 == null && newBitmapIn2 == null) {
                    showProgressDialog("重发优惠券", "提交中……");
                    netRequest.requestAddCoupon(1, SettingData.Post_AddCoupons3, s_name_edit, s_detail_edit,
                            couponBean.getPic_path(), "0", "0", "0", "0", s_name_edit, "0", "0", s_minconsumption, s_circulation_edit, s_data_tv, s_xiangou);
                    return;
                }
                showProgressDialog("添加优惠券", "提交中……");


                if (newBitmapIn1 != null && newBitmapIn2 != null) {
                    //两张图片
                    index=0;
                    netRequest.requestUploadPic(4, newBitmapIn1);
                } else {
                    //一张图片
                    InputStream in = null;
                    if (newBitmapIn1 != null) {
                        in = newBitmapIn1;
                    } else {
                        in = newBitmapIn2;
                    }
                    netRequest.requestUploadPic(4, in);
                }

//                new uploadtask().start();


            }
        });
    }

    private void showMyDialog(int type) {
        this.type = type;
        new AlertDialog.Builder(AddCoupons3_RebuildActivity.this)
                .setTitle("请选择图片来源")
                .setItems(new String[]{"相册", "相机"},
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                if (which == 0) {
                                    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                                    startActivityForResult(intent,
                                            FLAG_CHOOSE_IMG);
                                } else if (which == 1) {
                                    String status = Environment
                                            .getExternalStorageState();
                                    if (status
                                            .equals(Environment.MEDIA_MOUNTED)) {
                                        try {
                                            localTempImageFileName = "";
                                            localTempImageFileName = String
                                                    .valueOf((new Date())
                                                            .getTime())
                                                    + ".png";
                                            File filePath = SettingData.FILE_PIC_SCREENSHOT;
                                            if (!filePath.exists()) {
                                                filePath.mkdirs();
                                            }
                                            Intent intent = new Intent(
                                                    MediaStore.ACTION_IMAGE_CAPTURE);
                                            File f = new File(filePath,
                                                    localTempImageFileName);
                                            // localTempImgDir和localTempImageFileName是自己定义的名字
                                            Uri u = Uri.fromFile(f);
                                            intent.putExtra(
                                                    MediaStore.Images.Media.ORIENTATION,
                                                    0);
                                            intent.putExtra(
                                                    MediaStore.EXTRA_OUTPUT,
                                                    u);
                                            startActivityForResult(
                                                    intent,
                                                    FLAG_CHOOSE_PHONE);
                                        } catch (ActivityNotFoundException e) {
                                            //
                                        }
                                    }
                                }

                            }
                        }).show();
    }

    private void initView() {
        title_tv = (TextView) findViewById(R.id.tvTitle);
        right_lin = (LinearLayout) findViewById(R.id.public_ll_right);
        right_tv = (TextView) findViewById(R.id.public_tv_right);
        name_edit = (EditText) findViewById(R.id.addcoupns1_edit_name);
        detail_edit = (EditText) findViewById(R.id.addcoupns1_edit_detail);
        minconsumption_edit = (EditText) findViewById(R.id.addcoupns1_edit_minconsumption);
        circulation_edit = (EditText) findViewById(R.id.addcoupns1_edit_circulation);
        data_tv = (TextView) findViewById(R.id.addcoupns1_tv_date);
        img1 = (ImageView) findViewById(R.id.addcoupns1_img1);
        img2 = (ImageView) findViewById(R.id.addcoupns1_img2);
        post_tv = (TextView) findViewById(R.id.addcoupns1_tv_post);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == FLAG_CHOOSE_IMG && resultCode == RESULT_OK) {
            if (data != null) {
                Uri uri = data.getData();
                if (uri != null) {

                    CropUtils.startCropActivity(uri, AddCoupons3_RebuildActivity.this, 9, 5);
                }
            }
        } else if (requestCode == FLAG_CHOOSE_PHONE && resultCode == RESULT_OK) {
            File f = new File(SettingData.FILE_PIC_SCREENSHOT, localTempImageFileName);
            Uri uri = Uri.fromFile(f);
            if (uri != null) {
                //压缩图片
                UtilsBitmap.getCompressUri(uri);
                CropUtils.startCropActivity(uri, AddCoupons3_RebuildActivity.this, 9, 5);
            }
        } else if (requestCode == UCrop.REQUEST_CROP && resultCode == RESULT_OK) {
            //剪切成功
            if (data != null) {
                final Uri resultUri = UCrop.getOutput(data);
                bitmapBean = UtilsBitmap.addFilePath3(resultUri.getPath(), this);

                if (bitmapBean.getIsBm()!=null){
                    if (this.type == 1) {
                        newBitmapIn1 = bitmapBean.getIsBm();
                        img1.setImageBitmap(bitmapBean.getBitmap());
                    } else if (this.type == 2) {
                        newBitmapIn2 = bitmapBean.getIsBm();
                        img2.setImageBitmap(bitmapBean.getBitmap());
                    }
                }


            }
        } else if (resultCode == UCrop.RESULT_ERROR) {
            //剪切失败
            Toast.makeText(AddCoupons3_RebuildActivity.this, "图片剪切失败！", Toast.LENGTH_SHORT)
                    .show();
        }

    }

    @Override
    public void onAfinalFail() {
        dismissProgressDialog();
        Toast.makeText(AddCoupons3_RebuildActivity.this, "添加失败！", Toast.LENGTH_SHORT)
                .show();
    }

    String s_bitmap1 = null;
    String s_bitmap2 = null;
    String docResult = null;
    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        Log.v("zyl", "onExecuteSuccess:" + result);
        if (requestId == 1) {
            dismissProgressDialog();
            Toast.makeText(AddCoupons3_RebuildActivity.this, "添加成功！", Toast.LENGTH_SHORT)
                    .show();
            EventBus.getDefault().post(new CouponUpdateBean(1));
            finish();
        } else if (requestId == 2) {
            Toast.makeText(AddCoupons3_RebuildActivity.this, "提交成功！", Toast.LENGTH_SHORT)
                    .show();
            EventBus.getDefault().post(new CouponUpdateBean(1));
            finish();
        } else if (requestId == 3) {
            try {
                JSONObject jsonObject = new JSONObject(result);
                JSONArray jsonArray = jsonObject.getJSONArray("detail");
                JSONObject jsonObject_bean = (JSONObject) jsonArray.get(0);

                Gson gson = new Gson();
                couponBean = gson.fromJson(jsonObject_bean.toString(), CouponBean.class);
                if (couponBean == null) {
                    return;
                }
                name_edit.setText(couponBean.getTitle());
                detail_edit.setText(couponBean.getDesc1());
                circulation_edit.setText(couponBean.getIssued_num());
                minconsumption_edit.setText(couponBean.getLimit_amount());


                Map<String, Object> urlMap = GsonUtil.jsonToMap(jsonObject.getString("doc_url"));
                String picURL = couponBean.getPic_path();
                if (!TextUtils.isEmpty(picURL)) {
                    //说明有两张图片
                    if (picURL.contains(",")) {
                        couponBean.setUrl1(urlMap.get(picURL.split(",")[0]).toString());
                        couponBean.setUrl2(urlMap.get(picURL.split(",")[1]).toString());
                    } else {
                        couponBean.setUrl1(urlMap.get(picURL).toString());
                    }
                }
                ImageLoader.getInstance().displayImage(couponBean.getUrl1(), img1);
                ImageLoader.getInstance().displayImage(couponBean.getUrl2(), img2);

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }else if (requestId == 4) {
            //图片上传成功
            Log.d("lxp", "图片上传成功");
            try {

                JSONObject obj = new JSONObject(result);
                String doc_id = obj.getString("doc_id");
                Message msgImg = Message.obtain();
                msgImg.what = MSG_IMG_SUCC;

                if (newBitmapIn1 != null && newBitmapIn2 != null) {

                    //两张图片
                    if (index == 0) {
                        s_bitmap1 = doc_id;
                        index=1;
                        netRequest.requestUploadPic(4, newBitmapIn2);
                    } else if (index == 1) {
                        s_bitmap2 = doc_id;
                        if (!TextUtils.isEmpty(s_bitmap1) && !TextUtils.isEmpty(s_bitmap2)) {
                            docResult = s_bitmap1 + "," + s_bitmap2;
                            Log.d("lxp", "docResult======" + docResult);
                            msgImg.obj = docResult;
                            mHandler.sendMessage(msgImg);
                        }
                    }
                } else {
                    //一张图片
                    String[] d_coupon = couponBean.getPic_path().split(",");
                    if (d_coupon.length > 1) {
                        if (newBitmapIn1 != null) {
                            docResult = doc_id + "," + d_coupon[1];
                        } else {
                            docResult = d_coupon[0] + "," + doc_id;
                        }
                    } else {
                        if (newBitmapIn1 != null) {
                            docResult = doc_id;
                        } else {
                            docResult = couponBean.getPic_path() + "," + doc_id;
                        }
                    }
                    msgImg.obj = docResult;
                    mHandler.sendMessage(msgImg);
                }



            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        Log.v("zyl", "onExecuteFail:" + msg);
        if (requestId == 1) {
            dismissProgressDialog();
            Toast.makeText(AddCoupons3_RebuildActivity.this, "添加失败！", Toast.LENGTH_SHORT)
                    .show();
        } else if (requestId == 2) {
            Toast.makeText(AddCoupons3_RebuildActivity.this, "提交失败！", Toast.LENGTH_SHORT)
                    .show();
            finish();
        } else if (requestId == 3) {
            Toast.makeText(AddCoupons3_RebuildActivity.this, "获取优惠券失败！", Toast.LENGTH_SHORT)
                    .show();
            finish();
        }

    }

    private void showDatePicker() {
        Calendar mycalendar = Calendar.getInstance(Locale.getDefault());
        int year = mycalendar.get(Calendar.YEAR);
        int month = mycalendar.get(Calendar.MONTH);
        int day = mycalendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog dpd = new DatePickerDialog(AddCoupons3_RebuildActivity.this, datelistener, year, month, day);
        dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
        dpd.show();//显示DatePickerDialog组件
    }

    private DatePickerDialog.OnDateSetListener datelistener = new DatePickerDialog.OnDateSetListener() {
        /**params：view：该事件关联的组件
         * params：myyear：当前选择的年
         * params：monthOfYear：当前选择的月
         * params：dayOfMonth：当前选择的日
         */
        @Override
        public void onDateSet(DatePicker view, int myyear, int monthOfYear, int dayOfMonth) {
            //修改year、month、day的变量值，以便以后单击按钮时，DatePickerDialog上显示上一次修改后的值
            //更新日期
            Calendar mycalendar = Calendar.getInstance();
            mycalendar.set(Calendar.YEAR, myyear);
            mycalendar.set(Calendar.MONTH, monthOfYear);
            mycalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            s_data_tv = Utils.getTime(mycalendar) + " 23:59:59";
            data_tv.setText(s_data_tv.split(" ")[0]);
        }
    };

    /**
     * 上传图片
     */
    private class uploadtask extends Thread {

        public void run() {
            if (newbitmap1 != null && newbitmap2 != null) {
                String s_bitmap1 = null;
                String s_bitmap2 = null;
                for (int i = 0; i < 2; i++) {
                    Bitmap bitmap = null;
                    if (i == 0) {
                        bitmap = newbitmap1;
                    } else if (i == 1) {
                        bitmap = newbitmap2;
                    }

                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 10, stream);
                    InputStream in = new ByteArrayInputStream(stream.toByteArray());
                    DefaultHttpClient httpclient = new DefaultHttpClient();
                    try {
                        HttpPost httppost = new HttpPost(Stores.http
                                + "/ops-storage/storage/upload");

                        MultipartEntity reqEntity = new MultipartEntity();

                        reqEntity.addPart("app_token", MainActivity.app_token);
                        reqEntity.addPart("category", "oss_community");
                        reqEntity.addPart("subffix", "jpg");
                        reqEntity.addPart("up_load_file", "pic.jpg", in);
                        httppost.setEntity(reqEntity);
                        HttpResponse response = null;
                        response = httpclient.execute(httppost);
                        response.getStatusLine().toString();
                        String strResult = EntityUtils.toString(response.getEntity());
                        JSONObject obj = new JSONObject(strResult);

                        String rsp_code = obj.getString("rsp_code");
                        if (rsp_code.equals("succ")) {//图片上传成功


                            JSONObject data = obj.getJSONObject("data");
                            String doc_id = data.getString("doc_id");
                            if (i == 0) {
                                s_bitmap1 = doc_id;
                            } else if (i == 1) {
                                s_bitmap2 = doc_id;
                            }

                        } else {
                            Message msg = Message.obtain();
                            msg.what = MSG_IMG_FAILED;
                            msg.obj = "图片上传失败";
                            mHandler.sendMessage(msg);
                            return;
                        }
                        in.close();
                        stream.close();

                    } catch (Exception e) {
                        Message msg = Message.obtain();
                        msg.what = MSG_IMG_FAILED;
                        msg.obj = "图片上传失败";
                        mHandler.sendMessage(msg);
                        return;
                    }
                }
                if (!TextUtils.isEmpty(s_bitmap1) && !TextUtils.isEmpty(s_bitmap2)) {
                    Message msg = Message.obtain();
                    msg.what = MSG_IMG_SUCC;
                    msg.obj = s_bitmap1 + "," + s_bitmap2;
                    mHandler.sendMessage(msg);
                } else {
                    Message msg = Message.obtain();
                    msg.what = MSG_IMG_FAILED;
                    msg.obj = "图片上传失败";
                    mHandler.sendMessage(msg);
                    return;
                }
            } else {
                Bitmap bitmap = null;
                if (newbitmap1 != null) {
                    bitmap = newbitmap1;
                } else {
                    bitmap = newbitmap2;
                }
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 10, stream);
                InputStream in = new ByteArrayInputStream(stream.toByteArray());
                DefaultHttpClient httpclient = new DefaultHttpClient();
                try {
                    HttpPost httppost = new HttpPost(Stores.http
                            + "/ops-storage/storage/upload");

                    MultipartEntity reqEntity = new MultipartEntity();

                    reqEntity.addPart("app_token", MainActivity.app_token);
                    reqEntity.addPart("category", "oss_community");
                    reqEntity.addPart("subffix", "jpg");
                    reqEntity.addPart("up_load_file", "pic.jpg", in);
                    httppost.setEntity(reqEntity);
                    HttpResponse response = null;
                    response = httpclient.execute(httppost);
                    response.getStatusLine().toString();
                    String strResult = EntityUtils.toString(response.getEntity());
                    JSONObject obj = new JSONObject(strResult);

                    String rsp_code = obj.getString("rsp_code");
                    if (rsp_code.equals("succ")) {//图片上传成功


                        JSONObject data = obj.getJSONObject("data");
                        String doc_id = data.getString("doc_id");
                        Message msg = Message.obtain();
                        msg.what = MSG_IMG_SUCC;
                        String[] d_coupon = couponBean.getPic_path().split(",");
                        if (d_coupon.length > 1) {
                            if (newbitmap1 != null) {
                                msg.obj = doc_id + "," + d_coupon[1];
                            } else {
                                msg.obj = d_coupon[0] + "," + doc_id;
                            }
                        } else {
                            if (newbitmap1 != null) {
                                msg.obj = doc_id;
                            } else {
                                msg.obj = couponBean.getPic_path() + "," + doc_id;
                            }
                        }

                        mHandler.sendMessage(msg);
                        Log.v("zyl", "上传图片成功：" + doc_id);

                    } else {
                        Message msg = Message.obtain();
                        msg.what = MSG_IMG_FAILED;
                        msg.obj = "图片上传失败";
                        mHandler.sendMessage(msg);
                    }
                    in.close();
                    stream.close();

                } catch (Exception e) {
                    Message msg = Message.obtain();
                    msg.what = MSG_IMG_FAILED;
                    msg.obj = "图片上传失败";
                    mHandler.sendMessage(msg);
                }
            }
        }
    }
}
